package com.zhao.dota.task;

import com.zhao.dota.common.task.BaseTask;
import com.zhao.dota.model.Ability;
import com.zhao.dota.processor.AbilityProcessor;
import com.zhao.dota.service.AbilityService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class UpdateAbilityTask extends BaseTask {

    @Resource
    private AbilityProcessor abilityProcessor;
    @Resource
    private AbilityService abilityService;

    @Override
    protected void execute() {
        List<AbilityProcessor.Ability> abilityList = abilityProcessor.getAbilityList();
        List<Ability> abilities = abilityList.stream().map(a -> {
            Ability ability = new Ability();
            ability.setName(a.getName());
            ability.setDisplayName(a.getDisplayName());
            if (a.getUri() == null) {
                ability.setHeroName("");
            } else {
                ability.setHeroName(a.getUri());
            }
            return ability;
        }).collect(Collectors.toList());
        abilityService.updateList(abilities);
    }

    @Override
    protected String getKey() {
        return "abilityTask";
    }
}
